package com.demo.action;

import com.demo.domain.User;
import com.demo.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User> {

    private User user = new User();
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("struts...");
        return "success";
    }

    public String login() throws Exception {

        boolean success = userService.getUserByCodePassword(user);

        if (success) {

            ActionContext.getContext().getSession().put("user", user);
            return "toHome";
        } else {
            ActionContext.getContext().put("error", "用户名或密码错误");
            return "toLogin";
        }


    }

    @Override
            public User getModel() {
        return user;
    }
}
